<template>
  <BasicModal v-bind="$attrs" @register="registerModal" title="添加按钮权限" @ok="handleSubmit" :canFullscreen="false" :minHeight="120" :height="120">
    <BasicForm @register="registerForm"/>
  </BasicModal>
</template>
<script lang="ts">
import {defineComponent, ref, computed, unref} from 'vue'
import {BasicModal, useModalInner} from '@/components/modal'
import {BasicForm, useForm} from '@/components/form/index'
import {actionFormSchema} from '../data/role.data'
import {Api,saveAction} from '../data/role.api'

export default defineComponent({
  name: 'ActionModal',
  components: {BasicModal, BasicForm},
  emits: ['success', 'register'],
  setup(_, {emit}) {
    const isUpdate = ref(true)
    const menuId = ref('');
    const [registerForm, {resetFields, setFieldsValue, updateSchema, validate, formSubmit},] = useForm({
      rowKey: 'actionId',
      saveApi: Api.addAction,
      labelWidth: 100,
      schemas: actionFormSchema,
      showActionButtonGroup: false,
    })

    const [registerModal, {setModalProps, closeModal}] = useModalInner(
        async (data) => {
          await resetFields()
          menuId.value = data.menuId;
          setModalProps({confirmLoading: false})
          isUpdate.value = !!data?.isUpdate
        },
    )

    async function handleSubmit(callback) {
      //验证表并获取表单参数
      let values = await validate();
      values.menuId = menuId.value;
      //提交表单
      await saveAction(values);
      //prettier-ignore
      emit("success");
      closeModal();
    }

    return {registerModal, registerForm, handleSubmit}
  },
})
</script>
